package com.cheng.im.service.user.controller;

import com.cheng.im.service.common.resp.CommonResult;
import com.cheng.im.service.user.domain.ImUserData;
import com.cheng.im.service.user.model.req.*;
import com.cheng.im.service.user.model.resp.GetUserInfoResp;
import com.cheng.im.service.user.model.resp.ImportUserResp;
import com.cheng.im.service.user.model.resp.UserLoginResp;
import com.cheng.im.service.user.model.resp.UserOnlineStatusResp;
import com.cheng.im.service.user.service.ImUserDataService;
import com.cheng.im.service.user.service.ImUserStatusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Log
@Api(tags = "IM用户")
@RestController
@RequestMapping("/v1/user")
public class ImUserController {

    @Resource
    private ImUserDataService imUserDataService;
    @Resource
    private ImUserStatusService imUserStatusService;

    /**
     * 批量导入用户
     * @param req
     * @return
     */
    @ApiOperation(value = "批量导入用户")
    @PostMapping ("/importUser")
    public CommonResult<ImportUserResp> importUser(@RequestBody ImportUserReq req) {
        return imUserDataService.importUser(req);
    }

    /**
     * 批量删除用户
     * @param req
     * @return
     */
    @ApiOperation(value = "删除IM用户")
    @DeleteMapping("/deleteUser")
    public CommonResult<ImportUserResp> deleteUser(@RequestBody @Validated DeleteUserReq req) {
        return imUserDataService.deleteUser(req);
    }

    /**
     * 登录接口
     * @return
     */
    @ApiOperation(value = "登录接口", notes = "本不该放在这边的，应该放在其他模块，为了应付毕设写的")
    @PostMapping("/login")
    public CommonResult<UserLoginResp> login(@RequestBody UserLoginReq userLoginReq) {
        // todo: 登录不同端连接的节点不同
        return imUserDataService.userLogin(userLoginReq);
    }

    /**
     * 校验token
     */
    @PostMapping("/checkToken")
    @ApiOperation(value = "检查token", notes = "")
    public CommonResult checkToken(String token) {
        return imUserDataService.checkToken(token);
    }

    /**
     * 获取序列号
     * @param req
     * @return
     */
    @PostMapping("/getUserSequence")
    @ApiOperation(value = "获取序列号", notes = "")
    public CommonResult<Map<Object, Object>> getUserSequence(@RequestBody @Validated GetUserSequenceReq req) {
        return imUserDataService.getUserSequence(req);
    }


    @PostMapping("/queryFriendOnlineStatus")
    @ApiOperation(value = "查询好友在线状态", notes = "")
    public CommonResult<Map<String, UserOnlineStatusResp>> queryFriendOnlineStatus(@RequestBody @Validated PullFriendOnlineStatusReq req) {
        return CommonResult.success(imUserStatusService.queryFriendOnlineStatus(req));
    }



    /**
     * 批量获取用户信息
     * @param req
     * @return
     */
    @PostMapping("/getUserInfo")
    @ApiOperation(value = "批量获取用户信息", notes = "")
    public CommonResult<GetUserInfoResp> getUserInfo(@RequestBody GetUserInfoReq req){
        return imUserDataService.getUserInfo(req);
    }


    /**
     * 查看单个用户的信息
     * @param req
     * @return
     */
    @PostMapping("/getSingleUserInfo")
    @ApiOperation(value = "查看单个用户的信息", notes = "")
    public CommonResult<ImUserData> getSingleUserInfo(@RequestBody @Validated UserId req){
        return imUserDataService.getSingleUserInfo(req.getUserId(),req.getAppId());
    }

    /**
     * 修改用户资料
     * @param req
     * @return
     */
    @PutMapping("/modifyUserInfo")
    @ApiOperation(value = "修改用户资料", notes = "")
    public CommonResult modifyUserInfo(@RequestBody @Validated ModifyUserInfoReq req){
        return imUserDataService.modifyUserInfo(req);
    }


    /**
     * todo
     * 订阅用户在线状态
     * 设置客户端在线状态
     */
}
